package com.flyme.module.base.provider.api;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.common.core.security.OpenUserAuth;
import com.flyme.common.core.utils.ApiAssert;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.entity.SysMessage;
import com.flyme.module.base.client.entity.SysMessageSend;
import com.flyme.module.base.provider.service.SysMessageSendService;
import com.flyme.module.base.provider.service.SysMessageService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author flyme
 */
@Slf4j
@Api(tags = "app消息接口")
@RestController
@RequestMapping("/message")
public class ApiMessageController extends BaseController<SysMessageSendService, SysMessageSend> {

    @Autowired
    private SysMessageService messageService;
    @Autowired
    private SysMessageSendService messageSendService;

    /**
     * 消息首页
     */
    @RequestMapping(value = "/index")
    public ResultBody index() {
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        messageService.initSysMessage(userAuth.getUserId());
        EntityMap map = new EntityMap();
        EntityMap userMsg = bizService.getLastUserMessage(userAuth.getUserId());
        map.put("userMsg", userMsg);
        Integer userNum = bizService.countByStatus(userAuth.getUserId(), "2");
        map.put("userNum", userNum);
        EntityMap sysMsg = bizService.getLastSysrMessage(userAuth.getUserId());
        map.put("noticeMsg", sysMsg);
        Integer sysMsgNum = bizService.countByStatus(userAuth.getUserId(), "1");
        map.put("noticeNum", sysMsgNum);
        return ResultBody.success(map);
    }

    /**
     * 消息列表
     */
    @RequestMapping(value = "/message_list")
    public ResultBody messagList(String messageType, PagerInfo pagerInfo) {
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        Long userId = userAuth.getUserId();
        PqGrid pqGrid = new PqGrid();
        if (messageType.equals("1")) {
            pqGrid = getSysMessage(pagerInfo, userId, messageType);
            //messageSendService.updateByUser(userId,1);
        }
        if (messageType.equals("2")) {
            pqGrid = getUserMessage(pagerInfo, userId, messageType);
        }
        return ResultBody.success(pqGrid);
    }


    private PqGrid getSysMessage(PagerInfo pagerInfo, Long userId, String messageType) {
        CriteriaQuery cq = new CriteriaQuery(pagerInfo);
        cq.select("ms.id,message.id messageId,ms.messageType,ms.createTime,message.messageTitle,message.summary messageContent,ms.messageStatus");
        cq.eq("ms.messageType", messageType);
        cq.eq("ms.userId", userId);
        cq.createAlias("message", "message");
        cq.orderByDesc("ms.createTime");
        return bizService.pageList(cq);
    }

    private PqGrid getUserMessage(PagerInfo pagerInfo, Long userId, String messageType) {
        CriteriaQuery cq = new CriteriaQuery(pagerInfo);
        cq.select("ms.id,ms.messageType,ms.messageStatus,ms.targetId,ms.createTime,'订单支付成功提醒' orderTitle,ord.orderBody,ord.payDate,ord.id orderId");
        cq.eq("ms.messageType", messageType);
        cq.eq("ms.userId", userId);
        cq.createAlias("order", "ord");
        cq.orderByDesc("ms.createTime");
        return bizService.pageList(cq);
    }

    /**
     * 系统消息详情
     */
    @RequestMapping(value = "/getMessage")
    public ResultBody getMessage(ModelMap map, String noticeId) {
        ApiAssert.notNull("参数不能为空", noticeId);
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        Long userId = userAuth.getUserId();
//        SysMessageSend sysMessageSend = messageSendService.getById(noticeId);
//        SysMessage message = messageService.getById(sysMessageSend.getMessageId());
        SysMessage message = messageService.getById(noticeId);
        EntityMap html = new EntityMap();
        html.put("htmlTitle", message.getMessageTitle());
        html.put("content", message.getMessageContent());
        map.addAttribute("html", html);
        UpdateWrapper u = new UpdateWrapper();
        messageSendService.update(u.set(true, "messageStatus", 1).eq(true, "messageId", noticeId).eq(true, "userId", userId));
        return ResultBody.success(html);
    }

    /**
     * PC消息列表
     */
    @RequestMapping(value = "/messageListWeb")
    public ResultBody messageListWeb(String messageType, PagerInfo pagerInfo) {
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        EntityMap map = new EntityMap();
        Long userId = userAuth.getUserId();
        Integer userNum = bizService.countByStatus(userAuth.getUserId(), "2");
        map.put("userNum", userNum);
        Integer sysMsgNum = bizService.countByStatus(userAuth.getUserId(), "1");
        map.put("noticeNum", sysMsgNum);
        PqGrid pqGrid = new PqGrid();
        if (messageType.equals("1")) {
            pqGrid = getSysMessage(pagerInfo, userId, messageType);
        }
        if (messageType.equals("2")) {
            pqGrid = getUserMessage(pagerInfo, userId, messageType);
        }
        map.put("list", pqGrid);
        return ResultBody.success(map);
    }
}
